package com.example;

import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

/**
 * 表示HTML中a标签的类
 */
public class TagA {
    /* id */
    private String id;
    /* class:定义默认值，是一个空的Set集合 */
    /* final修饰的集合属性，属性值不能重新赋值，但是可以修改内部元素 */
    private final Set<String> classSet = new LinkedHashSet<>();
    /* title属性 */
    private String title;
    /* href属性 */
    private String href;
    /* target:_self和_blank */
    private String target;
    /* 样式属性:定义默认值，是一个空的Map集合 */
    private final Map<String, Object> style = new LinkedHashMap<>();
    /* a标签中的文本 */
    private String text;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    /*public Set<String> getClassSet() {
        return classSet;
    }*/

    /* 添加一个类名 */
    public TagA addClass(String className) {
        classSet.add(className);
        return this;
    }
    /* 移除一个类名 */
    public TagA removeClass(String className) {
        classSet.remove(className);
        return this;
    }

    /*public void setClassSet(Set<String> classSet) {
        this.classSet = classSet;
    }*/

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getHref() {
        return href;
    }

    public void setHref(String href) {
        this.href = href;
    }

    public String getTarget() {
        return target;
    }

    /* 通过枚举来限制参数 */
    public void setTarget(Target target) {
        switch (target) {
            case TOP:
                this.target = "_top";
                break;
            case PARENT:
                this.target = "_parent";
                break;
            case BLANK:
                this.target = "_blank";
                break;
            case SELF:
                this.target = "_self";
                break;
            default:
                break;
        }
    }

    public TagA addStyle(String key, Object value) {
        style.put(key, value);
        return this;
    }

    public TagA removeStyle(String key, Object value) {
        style.remove(key, value);
        return this;
    }

    /*public Map<String, Object> getStyle() {
        return style;
    }

    public void setStyle(Map<String, Object> style) {
        this.style = style;
    }*/

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    /* 将多个类名转换为空格分割的形式 */
    private String classStr() {
        StringBuilder sb = new StringBuilder();
        for (String className : classSet) {
            sb.append(className).append(" ");
        }
        // 去除最后一个空格
        return sb.substring(0, sb.toString().length() - 1);
    }

    private String styleStr() {
        StringBuilder sb = new StringBuilder();
        for (String key : style.keySet()) {
            sb.append(key)
                    .append(": ")
                    .append(style.get(key))
                    .append("; ");
        }
        return sb.substring(0, sb.toString().length() - 1);
    }

    @Override
    public String toString() {
        return "<a" +
                (id == null ? "" : " id='" + id + "'") +
                (classSet.isEmpty() ? "" : " class='" + classStr() + "'" ) +
                (href == null ? "" : " href='" + href + "'") +
                (title == null ? "" : " title='" + title + "'") +
                (target == null ? "" : " target='" + target + "'") +
                (style.isEmpty() ? "" : " style='" + styleStr() + "'") +
                ">" + text + "</a>";
    }
}
